package com.wh.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wh.constant.MessageConstant;
import com.wh.entity.PageResult;
import com.wh.entity.QueryPageBean;
import com.wh.entity.Result;
import com.wh.pojo.Poetry;
import com.wh.service.PoetryService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/poetry")
public class PoetryController {
    @Reference
    private PoetryService poetryService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = poetryService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //添加部门
    @RequestMapping("/add")
    public Result add(@RequestBody Poetry poetry){
        try {
            poetryService.add(poetry);
            return new Result(true, MessageConstant.ADD_POETRY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_POETRY_FAIL);
        }
    }

    //修改部门
    @RequestMapping("/edit")
    public Result edit(@RequestBody Poetry poetry){
        try {
            poetryService.edit(poetry);
            return new Result(true,MessageConstant.EDIT_POETRY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_POETRY_FAIL);
        }
    }

    //通过id找部门
    @RequestMapping("/findById")
    public Result findById(Integer id){
        System.out.println(id);
        try {
            Poetry poetry = poetryService.findById(id);
            return new Result(true, MessageConstant.QUERY_POETRY_SUCCESS,poetry);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_POETRY_FAIL);
        }
    }

    //删除部门
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            poetryService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_POETRY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_POETRY_FAIL);
        }
    }
}
